import json


def greet_user():
    # 如果以前存储了用户名，就加载问候用户
    # 否则，就提示用户输入用户名并存储

    username = get_stored_user()
    if username:
        print("Welcome back," + username + "!")
    else:
        username = get_new_username()
        print("We will remember you when you come back," + username + "!")


def get_new_username():
    """提示用户输入用户名"""
    username = input("What is your name? ")
    filename = 'json/username1.json'
    with open(filename, 'w') as f_obj:
        json.dump(username, f_obj)
    return username


def get_stored_user():
    # 如果存储了用户名，就获取它
    filename = "json/username1.json"
    try:
        with open(filename) as f_obj:
            username = json.load(f_obj)
    except FileNotFoundError:
        return None
    else:
        return username


greet_user()
